/*
 * Copyright (c) 2008 centrevillage <centrevillage@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#ifndef  __SEQCOMMAND_H__
#define  __SEQCOMMAND_H__

// seq
#include "VisCommand.h"
#include "Project.h"

namespace seq {

enum SeqCommandTypes {
    SEQ_CMD_INSERT= 1,
    SEQ_CMD_ERASE,
    SEQ_CMD_ERASE_RANGE,
    SEQ_CMD_CHANGE,
    SEQ_CMD_SET_SETUPSECTION
};

class InsertCommand : public VisCommand {
public:
    InsertCommand(ListCategory category, UINT32 tick, const EventPtr& pEvent) :
        category_(category),
        tick_(tick), 
        pEvent_(pEvent)
    {}
    InsertCommand(ListCategory category, UINT32 tick, Event* pEvent) :
        category_(category),
        tick_(tick), 
        pEvent_(EventPtr(pEvent))
    {}
    virtual ~InsertCommand() {}
    virtual void execute(Project& p) {
        p.insert(category_, tick_, pEvent_);
    }
    virtual void undo(Project& p) {
        p.erase(category_, tick_, *pEvent_);
    }
    virtual int typeID() const {
        return SEQ_CMD_INSERT;
    }
private:
    ListCategory category_;
    UINT32       tick_;
    EventPtr     pEvent_;
};

class EraseCommand : public VisCommand {
public:
    EraseCommand(ListCategory category, UINT32 tick, const EventPtr& pEvent) :
        category_(category),
        tick_(tick), 
        pEvent_(pEvent)
    {}
    EraseCommand(ListCategory category, UINT32 tick, Event* pEvent) :
        category_(category),
        tick_(tick), 
        pEvent_(pEvent)
    {}
    EraseCommand(ListCategory category, UINT32 tick, const Event& event) :
        category_(category),
        tick_(tick), 
        pEvent_(event.clone())
    {}
    virtual ~EraseCommand() {}
    virtual void execute(Project& p) {
        p.erase(category_, tick_, *pEvent_);
    }
    virtual void undo(Project& p) {
        p.insert(category_, tick_, pEvent_);
    }
    virtual int typeID() const {
        return SEQ_CMD_ERASE;
    }
private:
    ListCategory category_;
    UINT32       tick_;
    EventPtr     pEvent_;
};

class EraseRangeCommand : public VisCommand {
public:
    EraseRangeCommand(ListCategory category, UINT32 tickS, UINT32 tickE) :
        category_(category),
        tickS_(tickS), 
        tickE_(tickE)
    {}
    virtual ~EraseRangeCommand() {}
    virtual void execute(Project& p) {
        deleted_ = p.get(category_, tickS_, tickE_);
        p.eraseRange(category_, tickS_, tickE_);
    }
    virtual void undo(Project& p) {
        for (size_t i = 0; i < deleted_.size(); ++i) {
            p.insert(category_, deleted_[i].first, deleted_[i].second);
        }
    }
    virtual int typeID() const {
        return SEQ_CMD_ERASE_RANGE;
    }
private:
    ListCategory category_;
    UINT32 tickS_;
    UINT32 tickE_;
    std::vector<EventList::elem_type> deleted_;
};

class ChangeCommand : public VisCommand {
public:
    ChangeCommand(ListCategory category, UINT32 tick, const EventPtr& pEvent, const EventPtr& pNewEvent) :
        category_(category),
        tick_(tick), 
        pEvent_(pEvent),
        pNewEvent_(pNewEvent)
    {}
    virtual ~ChangeCommand() {}
    virtual void execute(Project& p) {
        if (p.find(category_, tick_, *pEvent_)) {
            p.erase(category_, tick_, *pEvent_);
            p.insert(category_, tick_, pNewEvent_);
        }
    }
    virtual void undo(Project& p) {
        if (p.find(category_, tick_, *pNewEvent_)) {
            p.erase(category_, tick_, *pNewEvent_);
            p.insert(category_, tick_, pEvent_);
        }
    }
    virtual int typeID() const {
        return SEQ_CMD_CHANGE;
    }
    const EventPtr& oldEvent() const {return pEvent_;}
    const EventPtr& newEvent() const {return pNewEvent_;}
    ListCategory listCategory() const {return category_;}
    unsigned tick() const {return tick();}
private:
    ListCategory category_;
    UINT32       tick_;
    EventPtr     pEvent_;
    EventPtr     pNewEvent_;
};

class SetSetupSectionCommand : public VisCommand {
public:
    SetSetupSectionCommand(ListCategory category, Event* pEvent) :
        category_(category),
        pEvent_(pEvent),
        pOldEvent_((Event*)0)
    {}
    SetSetupSectionCommand(ListCategory category, const EventPtr& pEvent) :
        category_(category),
        pEvent_(pEvent),
        pOldEvent_((Event*)0)
    {}
    virtual ~SetSetupSectionCommand() {}
    virtual void execute(Project& p) {
        pOldEvent_ = p.setupSection.getEvent(category_);
        if (pEvent_) {
            p.setupSection.setEvent(category_, pEvent_);
        } else {
            p.setupSection.eraseEvent(category_);
        }
    }
    virtual void undo(Project& p) {
        if (pOldEvent_) {
            p.setupSection.setEvent(category_, pOldEvent_);
        } else {
            p.setupSection.eraseEvent(category_);
        }
    }
    virtual int typeID() const {
        return SEQ_CMD_SET_SETUPSECTION;
    }
    const EventPtr& oldEvent() const {return pOldEvent_;}
    const EventPtr& newEvent() const {return pEvent_;}
    ListCategory listCategory() const {return category_;}
private:
    ListCategory category_;
    EventPtr     pEvent_;
    EventPtr     pOldEvent_;
};

//typedef InsertCommand<SEQ_CMD_INSERT_NOTE, NoteEvent> InsertNoteCommand;
//typedef EraseCommand<SEQ_CMD_INSERT_NOTE, NoteEvent> EraseNoteCommand;
//typedef ChangeCommand<SEQ_CMD_INSERT_NOTE, NoteEvent> ChangeNoteCommand;
//typedef InsertCommand<SEQ_CMD_INSERT_CTL, ControlEvent> InsertControlCommand;
//typedef EraseCommand<SEQ_CMD_INSERT_CTL, ControlEvent> EraseControlCommand;
//typedef ChangeCommand<SEQ_CMD_INSERT_CTL, ControlEvent> ChangeControlCommand;
//typedef InsertCommand<SEQ_CMD_INSERT_ONE_VALUE, OneIntValueEvent> InsertOneIntValueCommand;
//typedef EraseCommand<SEQ_CMD_INSERT_ONE_VALUE, OneIntValueEvent> EraseOneIntValueCommand;
//typedef ChangeCommand<SEQ_CMD_INSERT_ONE_VALUE, OneIntValueEvent> ChangeOneIntValueCommand;

}

#endif   /* ----- #ifndef __SEQCOMMAND_H__  ----- */
